/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.biz.domain.SensorLog;
import me.zhengjie.modules.biz.service.SensorLogService;
import me.zhengjie.modules.biz.service.SensorService;
import me.zhengjie.modules.biz.service.dto.SensorLogQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author Admin
* @date 2023-02-17
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "sensor-log管理")
@RequestMapping("/api/sensorLog")
public class SensorLogController {

    private final SensorLogService sensorLogService;
    private final SensorService sensorService;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('sensorLog:list')")
    public void exportSensorLog(HttpServletResponse response, SensorLogQueryCriteria criteria) throws IOException {
        sensorLogService.download(sensorLogService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询sensor-log")
    @ApiOperation("查询sensor-log")
//    @PreAuthorize("@el.check('sensorLog:list')")
    public ResponseEntity<Object> querySensorLog(SensorLogQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(sensorLogService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增sensor-log")
    @ApiOperation("新增sensor-log")
//    @PreAuthorize("@el.check('sensorLog:add')")
    public ResponseEntity<Object> createSensorLog(@Validated @RequestBody SensorLog resources){
        return new ResponseEntity<>(sensorLogService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改sensor-log")
    @ApiOperation("修改sensor-log")
//    @PreAuthorize("@el.check('sensorLog:edit')")
    public ResponseEntity<Object> updateSensorLog(@Validated @RequestBody SensorLog resources){
        sensorLogService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除sensor-log")
    @ApiOperation("删除sensor-log")
//    @PreAuthorize("@el.check('sensorLog:del')")
    public ResponseEntity<Object> deleteSensorLog(@RequestBody Integer[] ids) {
        sensorLogService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/environmentLog")
    @ApiOperation("环境日志")
    @AnonymousAccess
    public ResponseEntity<Object>queryEnvironmentLog(SensorLogQueryCriteria criteria,Pageable pageable){
        return new ResponseEntity<>(sensorLogService.queryEnvironment(criteria,pageable),HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping(value = "/statistics")
    public ResponseEntity<Object> querySensorLogByTime(){
        ArrayList<Integer> sensorId = sensorService.queryAllByTimeAndId();
        sensorLogService.queryAllByTime(sensorId);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/charts")
    @AnonymousAccess
    public ResponseEntity<Object> charts(HttpServletRequest request){
        return new ResponseEntity<>(sensorLogService.charts(request),HttpStatus.OK);
    }


//    @GetMapping("/querySensorLogByType")
//    @PreAuthorize("@el.check('sensorLog:list')")
//    public ResponseEntity<Object> querySensorLogByType(SensorLogQueryCriteria criteria, Pageable pageable){
//        return new ResponseEntity<>(sensorLogService.querySensorLogByType(criteria,pageable),HttpStatus.OK);
//    }
}
